package com.youlai.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.system.common.result.PageResult;
import com.youlai.system.common.result.Result;
import com.youlai.system.enums.LogModuleEnum;
import com.youlai.system.model.entity.SysCheckItem;
import com.youlai.system.model.form.CheckItemForm;
import com.youlai.system.model.form.ConfigForm;
import com.youlai.system.model.query.CheckItemPageQuery;
import com.youlai.system.plugin.syslog.annotation.LogAnnotation;
import com.youlai.system.service.SysCheckItemService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @author yao
 * @create 2024/8/23 上午10:06
 */
@Tag(name = "检查项目设置")
@RestController
@RequestMapping("/api/v1/CheckItem")
@RequiredArgsConstructor
public class SysCheckItemController {
    private final SysCheckItemService sysCheckItemService;


    @Operation(summary = "获取检查项目列表")
    @GetMapping("/page")
    @LogAnnotation(value = "检查项目分页列表", module = LogModuleEnum.USER)
    public PageResult<SysCheckItem> listPagedUsers(
            CheckItemPageQuery queryParams
    ) {
        IPage<SysCheckItem> result = sysCheckItemService.listPagedCheckItems(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "添加检查项目")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('sys:checkItem:add')")
    public Result<?> addSysCheckItems(@RequestBody @Valid SysCheckItem sysCheckItem)
    {
        return Result.judge(sysCheckItemService.save(sysCheckItem));
    }

    @Operation(summary = "获取检查项目详情配置表单数据")
    @GetMapping("/{checkItemId}/form")
    public Result<SysCheckItem> getCheckItemForm(@Parameter(description = "检查项目ID") @PathVariable Long checkItemId) {
        SysCheckItem sysCheckItem = sysCheckItemService.getById(checkItemId);
        return Result.success(sysCheckItem);
    }

    @Operation(summary = "修改检查项目")
    @PutMapping("/{checkItemId}")
    @PreAuthorize("@ss.hasPerm('sys:checkItem:edit')")
    public Result updateCheckItem(@PathVariable Long checkItemId, @RequestBody @Valid CheckItemForm form) {
        SysCheckItem checkItem = new SysCheckItem();
        checkItem.setCheckItemId(checkItemId);
        checkItem.setCheckItemName(form.getCheckItemName());
        checkItem.setKeywords(form.getKeyword());
        checkItem.setUnitPrice(form.getUnitPrice());
        checkItem.setCost(form.getCost());
        checkItem.setUnit(form.getUnit());
        checkItem.setTypeId(form.getTypeId());
        checkItem.setStatus(form.getStatus());
        return Result.judge(sysCheckItemService.updateById(checkItem));
    }




}
